<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Db\ResultSet\HydratingResultSet;

class IndexController extends AbstractActionController
{
    public function indexAction()
    {
        $adapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
        
        $tableGateway = new \Zend\Db\TableGateway\TableGateway('modules', $adapter, null, new HydratingResultSet());
        
        $resultSet = $tableGateway->select(array('state' => 1))->toArray();
        
        $modules = array();
        
        $viewModel = new ViewModel();
        
        foreach ($resultSet as $row)
        {
        	$module_name = $row['key_name'];
        	
        	$modules[] = $module_name;
        	
        	$introViewModel = $this->forward()->dispatch($module_name.'\Controller\Index', array('action' => 'index'));
        	
        	$viewModel->addChild($introViewModel, $module_name);
        }
        
        $viewModel->setVariable('modules', $modules);
    	
        
    	return $viewModel;
    }
}
